﻿using CommunityToolkit.Mvvm.Input;
using JIG_SoftTemplate.Common.Services;
using JIG_SoftTemplate.Models;

namespace JIG_SoftTemplate.ViewModels
{
    public partial class MainWindowViewModel : ViewModelBase
    {
        private readonly Common.Services.NavigationService navigationService;
        private readonly JsonConfigService jsonConfigService;
        private readonly LangSwitchService langSwitchService;

        public MainWindowViewModel(Common.Services.NavigationService navigationService, JsonConfigService jsonConfigService, LangSwitchService langSwitchService)
        {
            this.navigationService = navigationService;
            this.jsonConfigService = jsonConfigService;
            this.langSwitchService = langSwitchService;
        }


        private async Task LoadAppConfig()
        {
            //读取配置文件
            var loadedConfig = await jsonConfigService.LoadConfigAsync(new AppConfigModel());

            if (loadedConfig != null)
            {
                if (loadedConfig?.Common != null)
                {
                    //刷新显示语言
                    langSwitchService.LangSwitch(loadedConfig.Common.SelectedLang);
                }
            }
        }


        #region 中继命令

        /// <summary>
        /// 载入命令
        /// </summary>
        [RelayCommand]
        public async Task Loaded()
        {
            await LoadAppConfig();
            navigationService.Navigate<HomePageViewModel>();
        }

        #endregion
    }
}
